#coding:utf-8
from . import pay
from .. import model ,db
from flask import  render_template,request,url_for,redirect,session
from ..model import  Users,Teacher,Kinds,Courses,Videos
import random,datetime

@pay.route('/login')
def login():
    sada=session.get('sada')
    name=session.get('name')
    print  sada,name
    return  render_template('/login.html',sada=session.get('sada'),name=session.get('name'))

@pay.route('/add',methods=['POST'])
def add():
    name=request.form['name']
    psw=request.form['psw']
    confirm=request.form['confirm']
    user = db.session.query(Users).filter(Users.name==name).first()
    if not user and psw==confirm:
        cla=Users(name=name,password=psw)
        db.session.add(cla)
        db.session.commit()
    return redirect(url_for('.entry'))



@pay.route('/entry')
def entry():
    return render_template('entry.html',sada=session.get('sada'),name=session.get('name'))

# 与登录的数据进行比较
@pay.route('/land',methods=['POST'])
def land():
    name=request.form['name']
    psw=request.form['psw']
    user=db.session.query(Users).filter(Users.name==name).first()
    if user and user.verify_psw(psw):
        session['sada']=True
        session['name']=name
        return redirect(url_for('main.postr'))
    return '登录失败请重新登录'


# 注销 
@pay.route('/loginout')
def loginout():
    session['sada']=False
    session.pop('name')
    return redirect(url_for('main.postr'))






















# @pay.route('/adds')
# def adds():

    # for item in range(1,8):
    #     name='name_%s'%item
    #     user=model.Kinds(name=name)
    #     db.session.add(user)
    #     db.session.commit()



    # for item in range(1,20):
    #     title='name_%s'%item
    #     abstract='简介_%s'%random.randint(1,20)
    #     addtime=datetime.datetime.now()
    #     k_id=random.randint(1,8)
    #     user=model.Courses(title=title,abstract=abstract,addtime=addtime,k_id=k_id)
    #     db.session.add(user)
    #     db.session.commit()





    # na=['python','PHP教育','H5坑','JAVA啥','VR好东西','C++不会','WEDkeyi','GIT啥啊']
    # for ne in range(30):
    #     title=ne
    #     time=random.randint(20,60)
    #     hist=random.randint(60,100)
    #     abstract='介绍简介%s'%random.randint(10,20)
    #     t_id=random.randint(1,7)

    #     addtime=datetime.datetime.now()
    #     c_id=random.randint(1,15)
    #     user=model.Videos(title=title,time=time,hist=hist,abstract=abstract,t_id=t_id,addtime=addtime,c_id=c_id)
    #     db.session.add(user)
    #     db.session.commit()


    # for item in range(1,8):
    #     name='t_%s'%item
    #     abstract='t_abstract'*random.randint(1,10)
    #     num=random.randint(0,1)
    #     if num==0:
    #         level='助教'
    #     else:
    #         level='讲师'
    #     teacher=model.Teacher(name=name,abstract=abstract,level=level)

    #     db.session.add(teacher)
    #     db.session.commit()
    # return '添加成功'